<template>
  <div class="standard-container layout-padding layout-pd">
    <div class="review-list-title">
      <div class="title-actions">
        <el-button size="default" type="primary" class="ml10" @click="refresh">
          <el-icon>
            <ele-Search/>
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="primary" class="ml10" @click="onOpenTemplate('add', '')">
          <el-icon>
            <ele-FolderAdd/>
          </el-icon>
          新增模版
        </el-button>
      </div>
    </div>

    <div class="template-main mar15">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="3" class="mb15" v-for="(item, index) in state.tableData.data">
          <div class="template cursor-pointer">
            <div class="template-used">
              <el-button plain round type="warning" @click.top="useTemplate(item.id)" v-if="item.used == 0"> 使用
              </el-button>
              <el-button type="danger" plain round disabled v-else>已使用</el-button>
            </div>
            <div class="template-content">
              <el-image :src="STATIC_URL + item.image" :fit="fit" class="template-preview" v-if="item.image"/>
            </div>
            <div class="template-info">
              <div class="title">{{ item.name }}</div>
              <div class="remark">
                <div>备注:</div>
                <div class="remark-content" :title="item.remark">{{ item.remark }}</div>
              </div>
              <div class="update-time">
                <div>更新时间:</div>
                <div>{{ item.updateTime }}</div>
              </div>
              <div class="operation">
                <div class="btn">
                  <el-button size="small" text type="info" @click="onDecoration(item.id)">装修</el-button>
                  <el-button size="small" text type="info" @click="onOpenTemplate('edit', item.id)">编辑</el-button>
                </div>
                <div class="btn">
                  <el-button size="small" text type="info">复制</el-button>
                  <el-button size="small" text type="danger" @click="onRowDel(item)">删除</el-button>
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="pagination-container">
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          :page-sizes="[10, 15, 20, 30, 50, 100]"
          v-model:current-page="state.tableData.paging.currentPage"
          background
          v-model:page-size="state.tableData.paging.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.paging.totalCount"
      >
      </el-pagination>
    </div>

    <DialogTemplate ref="DialogTemplateRef" @refresh="refresh"></DialogTemplate>
  </div>
</template>

<script setup lang="ts" name="ShopDecorationTemp">
import {defineAsyncComponent, reactive, onMounted, ref, watch} from 'vue';
import {useRouter} from 'vue-router';
import {decorationApi} from '/@/api/shop/decoration';
import {ElMessageBox, ElMessage} from 'element-plus';

const DialogTemplate = defineAsyncComponent(() => import('./dialog.vue'));
const DialogTemplateRef = ref();
import msg from '/@/utils/messageUtils';
import {STATIC_URL} from '/@/utils/config';
import DEMO_IMG from '/@/assets/temp/123.png';
import messageUtils from '/@/utils/messageUtils';

const router = useRouter();
const state = reactive({
  isToggle: false,
  choiceIds: [],
  search: {
    name: '',
  },
  tableData: {
    data: [],
    total: 0,
    loading: false,
    paging: {
      currentPage: 1,
      pageSize: 15,
      totalCount: 0,
      totalPages: 0,
    },
  },
});

const getTableData = async () => {
  state.tableData.loading = true;
  try {
    const {data} = await decorationApi().search({
      offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
      limit: state.tableData.paging.pageSize,
      name: state.search.name,
    });
    state.tableData.data = data.datas;
    state.tableData.paging.currentPage = data.paging.currentPage;
    state.tableData.paging.totalCount = data.paging.totalCount;
  } catch (error) {
    msg().error('查询失败，请稍后重试');
  }
  state.tableData.loading = false;
};

const fit = ref('cover');
const handleSelectionPer = (choiceIds: any) => {
  state.choiceIds = [];
  choiceIds.forEach((e) => state.choiceIds.push(e.id));
};

// 分页改变
const onHandleSizeChange = (pageSize: number) => {
  state.tableData.paging.pageSize = pageSize;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
  state.tableData.paging.currentPage = currentPage;
  getTableData();
};

const onOpenPreview = (row: any) => {
  console.log('onOpenPreview ', row);
};

const onDecoration = (id: string) => {
  console.log('onOpenEdit: ', id);
  router.push({
    path: `/shop/decoration`,
    query: {
      id: id,
    },
  });
};

const onOpenTemplate = (flag: string, id: string) => {
  DialogTemplateRef.value.openDialog(flag, id);
};

const useTemplate = async (id: string) => {
  try {
    await decorationApi().used(id);
    refresh();
    msg().success('操作成功');
  } catch (e) {
    msg().error('设置失败，请稍后重试');
  }
};

const onRowDel = (row: any) => {
  console.log('row: ', row);

  ElMessageBox.confirm(`此操作将永久删除模版名称：“${row.name}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async () => {
        await decorationApi().delete(row.id);
        msg().success('操作成功');
        refresh();
      })
      .catch(() => {
      });
};

const refresh = () => {
  onHandleCurrentChange(1);
};
onMounted(() => {
  console.log('init');
  getTableData();
});
</script>

<style scoped lang="scss">
.template-layout {
  background-color: #fff;
}

.template-main {
  flex: 1;
}

.template {
  height: 480px;
  transition: all 0.3s;
  border-radius: 15px;
  border: 1px solid var(--next-border-color-light);
  box-shadow: 0 0 4px 2px #59595933;
  overflow: hidden;
  position: relative;

  &:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 16px #5959593d;

    .template-info {
      display: block;
    }

    .template-used {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .template-used {
    position: absolute;
    display: none;
    height: 50px;
    width: 100%;
    left: 0px;
    top: 0px;
    z-index: 100;
    background-color: rgba(223, 230, 233, 0.8);
  }

  .template-content {
  }

  .template-preview {
    height: 100%;
    width: 100%;
  }

  .template-info {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 0 12px;
    transition: all 0.3s;
    background-color: #f1f2f6;

    .title {
      padding-top: 10px;
    }

    .remark {
      color: var(--next-color-seting-header);
      width: 100%;
      display: flex;
      height: 20px;
      align-items: center;

      .remark-content {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        flex: 1;
      }
    }

    .update-time {
      font-size: 12px;
      color: var(--next-color-seting-header);
      display: flex;
      height: 20px;
      align-items: center;
    }

    .operation {
      display: flex;
      justify-content: space-between;
      height: 30px;
      align-items: center;

      .btn {
        display: flex;

        button {
          margin-left: 0px;
          // padding: 5px;
        }
      }
    }
  }
}
</style>
